/*package com.glgc.projecti.web.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.glgc.projecti.common.BizException;
import com.glgc.projecti.common.CommonConstant;
import com.glgc.projecti.common.CommonUtils;
import com.glgc.projecti.common.WeixinUtils;
import com.glgc.projecti.entity.Award;
import com.glgc.projecti.entity.Nmq;
import com.glgc.projecti.entity.OpenUser;
import com.glgc.projecti.entity.WinningList;
import com.glgc.projecti.entity.dto.AwardDto;
import com.glgc.projecti.entity.dto.WinningListDto;
import com.glgc.projecti.service.INmqService;
import com.glgc.projecti.service.IOpenUserService;
import com.glgc.projecti.service.ISharingService;
import com.glgc.projecti.service.IWinningListService;
import com.glgc.projecti.web.controller.base.BaseController;

@Controller
@RequestMapping("/nmq")
public class NmqController extends BaseController {

	private Logger logger = Logger.getLogger(this.getClass());

	private static final String dateStr = "20151202";
	
	@Resource
	private INmqService nmqService;

	@Resource
	private IWinningListService winningListService;

	@Resource private ISharingService sharingService;
	
	@Resource
	IOpenUserService openUserService;
	
	@Value("#{config.corrent_answer}")
	private String correntAnswer;
	
	private boolean winningFlag = false; 

	// 问卷页
	@RequestMapping("/index.do")
	public String index(String sourceType, String sourceId,
			RedirectAttributes attr, HttpSession session,
			HttpServletRequest request, Model model) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		if (!CommonUtils.isEmpty(sourceType)) {
			session.setAttribute("sourceType", sourceType);
		}
		model.addAttribute("wish", wish);
		return "../question/index_question";
	}
	
	// 问卷页
	@RequestMapping("/share.do")
	public String share(String sourceType, String sourceId,
			RedirectAttributes attr, HttpSession session,
			HttpServletRequest request, Model model) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		if (!CommonUtils.isEmpty(sourceType)) {
			session.setAttribute("sourceType", sourceType);
		}
		model.addAttribute("wish", wish);
		return "../question/wish_question";
	}

	// 问卷抽奖页
	@RequestMapping("/wheel.do")
	public String wheel(String sourceType, String sourceId,
			RedirectAttributes attr, HttpSession session,
			HttpServletRequest request, Model model) throws IOException {
		Map<String, String> wish = WeixinUtils.wish(request);
		if (!CommonUtils.isEmpty(sourceType)) {
			session.setAttribute("sourceType", sourceType);
		}
		model.addAttribute("wish", wish);
		return "../question/wheel_question";
	}

	@RequestMapping("/save")
	public @ResponseBody JSONObject save(Nmq nmq, HttpSession session) throws ParseException {
		 String openId = (String)session.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		 if (openId == null) {
			 return this.toJSONResult(false, CommonConstant.ERROR_NO_OPEN_ID);
		 }
		logger.info("wechar_id is " + openId);

		Date timeLine = getTimeline();
		
		List<Nmq> temp = nmqService.findByWechatIdAfterTime(openId, timeLine);
		if (CommonUtils.isListEmpty(temp)) {
			if (winningFlag) {
				WinningList winningList = new WinningList();
				winningList.setIsReceive(0);
				winningList.setIsSend(0);
				winningList.setWinSource(1);
				winningList.setOpenId(openId);
				winningListService.save(winningList);
				logger.info("create winning(" + winningList.getId() + ") success!!");
			}
		} else {
			return this.toJSONResult(false, CommonConstant.ERROR_NO_HAVE_ANSWERED);
		}
		

		nmq.setWechatId(openId);
		Nmq temptemp = nmqService.save(nmq);
		logger.info("create/update nmq(" + nmq.getId() + ") success!!");
		
		Nmq dto = new Nmq();
		dto.fillDate();
		dto.setId(temptemp.getId());
		dto.setAnswer(correntAnswer);
		
		return this.toJSONResult(true, dto);
	}

	
	*//**
	 * 设置一个时间点，查询之后的nmq
	 * @return timeline  Date类型时间点
	 * @throws ParseException
	 *//*
	private Date getTimeline() throws ParseException {
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date dateLine = format.parse(dateStr);
		return dateLine;
	}
	
	// 更新评论
	@RequestMapping("/updateComments.do")
	public @ResponseBody JSONObject updateComments(Nmq dto) {
		nmqService.updateComment(dto.getId(), dto.getComments());
		logger.info("update nmq(" + dto.getId() + ") comment : " + dto.getComments());
		return this.toJSONResult(true);
	}

	// 分享链接
	@RequestMapping("/getWinCount.do")
	public @ResponseBody JSONObject getWinCount(HttpSession session) {
		String openId = (String)
				 session.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		if (openId == null) {
			return this.toJSONResult(true, 0);
		}
		int winCount = winningListService.getWinCount(openId);
		logger.info("find user(" + openId + ") win count: " + winCount
				+ " times");
		return this.toJSONResult(true, winCount);
	}

	// 抽奖动作
	@RequestMapping("/win.do")
	public @ResponseBody JSONObject win(HttpSession session) {
		String openId = (String) session
				.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		if (openId == null) {
			return this.toJSONResult(false, CommonConstant.ERROR_NO_OPEN_ID);
		}
		String isOpenUserFlag = "1";
		OpenUser tempUser = openUserService.findByOpenId(openId);
		if (tempUser == null) { // 没有登记
			isOpenUserFlag = "0";
		}
		Award award = null;
		
		List<Nmq> list = nmqService.findByWechatId(openId);
		if (CommonUtils.isListEmpty(list)) {
			return this.toJSONResult(false);
		}
		Nmq nmq = list.get(0);
		boolean flag = false;
		if(nmq.getAnswer().equals(correntAnswer)) {
			flag = true;
		}
		try {
			award = winningListService.win(openId, flag);
		} catch (BizException e) {
			return this.toJSONResult(false, e.getErrInfo());
		}
		if (award == null) {
			return this.toJSONResult(true);
		} else {
			AwardDto dto = new AwardDto().convertToDto(award);
			dto.setIsOpenUser(isOpenUserFlag);
			if (tempUser != null) {
				dto.setUsername(tempUser.getUserName());
				dto.setMobile(tempUser.getMobile());
			}
			return this.toJSONResult(true, dto);
		}
	}

	@RequestMapping("/getOpenUser.do")
	public @ResponseBody JSONObject getOpenUser(HttpSession session) {
		String openId = (String) session
				.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		if (openId == null) {
			return this.toJSONResult(false, CommonConstant.ERROR_NO_OPEN_ID);
		}
		OpenUser ou = this.openUserService.findByOpenId(openId);
		return this.toJSONResult(true, ou);
	}

	// 查看历史记录
	@RequestMapping("/viewHistory.do")
	public @ResponseBody JSONObject viewHistory(HttpSession session) {
		String openId = (String) session
				.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		if (openId == null) {
			return this.toJSONResult(false, CommonConstant.ERROR_NO_OPEN_ID);
		}
		List<WinningListDto> winList = winningListService.findNmqHistory(openId);
		return this.toJSONResult(true, winList);
	}

	
	// 分享链接
	@RequestMapping("/share")
	public @ResponseBody JSONObject share(HttpSession session) {
		String openId = (String) session
				.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		if (openId == null) {
			return this.toJSONResult(false, CommonConstant.ERROR_NO_OPEN_ID);
		}
		sharingService.share(openId);
		return this.toJSONResult(true);
	}
	
	
	@RequestMapping("/updateOpenUser.do")
	public @ResponseBody JSONObject updateOpenUser(String username, String mobile, HttpSession session) {
		String openId = (String) session
				.getAttribute(CommonConstant.WEIXING_OPEN_ID);
		if (openId == null) {
			return this.toJSONResult(false, CommonConstant.ERROR_NO_OPEN_ID);
		}
		OpenUser openUser = new OpenUser();
		openUser.setOpenId(openId);
		openUser.setUserName(username);
		openUser.setMobile(mobile);
		openUserService.saveOrUpdate(openUser);
		return this.toJSONResult(true);
	}
}
*/